package main

import (
	"expvar"
	"fmt"
	"net/http"
	"time"
)

var (
	messageCount = expvar.NewInt("message_count")
)

func main() {
	go incrementMessages()

	http.HandleFunc("/metrics", expvarHandler)
	fmt.Println("Starting server on :8080")
	http.ListenAndServe(":8080", nil)
}

func incrementMessages() {
	for {
		messageCount.Add(1)
		time.Sleep(time.Second)
	}
}

func expvarHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, expvar.Get("message_count").String())
}
